home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJSRC111.ZIP / go32 / fixstub.c < prev    next >
C/C++ Source or Header  |  1993-10-17  |  3KB  |  136 lines

  1. /* This is file FIXSTUB.C */
  2. /*
  3. ** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20.  
  21. #include "gotypes.h"
  22. #include "stubinfo.h"
  23.  
  24. word32 offset_of_info = 0;
  25. word32 size_of_info = 0;
  26.  
  27. StubInfo stub_info = {
  28.   STUB_INFO_MAGIC
  29. };
  30.  
  31. char *key = stub_info.magic;
  32.  
  33. void find_info(char *filename)
  34. {  
  35.   FILE *f;
  36.   int ch;
  37.   int key_p = 0;
  38.   long key_max, key_cnt=0;
  39.   unsigned char header[6];
  40.  
  41.   f = fopen(filename, "rb");
  42.   if (f == 0)
  43.   {
  44.     char buf[100];
  45.     sprintf(buf, "Fatal error in stubedit reading %s", filename);
  46.     perror(buf);
  47.     exit(1);
  48.   }
  49.  
  50.   fread(header, 6, 1, f);
  51.   key_p += 6;
  52.   key_max = header[4] + header[5]*256 + 1;
  53.   key_max *= 512;
  54.  
  55.   while ((ch = fgetc(f)) != EOF)
  56.   {
  57.     if (ch == key[key_p])
  58.     {
  59.       key_p++;
  60.       if (key[key_p] == 0)
  61.       {
  62.         fgetc(f); /* the NULL */
  63.         offset_of_info = ftell(f) - 16; /* skip the NULL in the file */
  64.         fread(&size_of_info, 1, 4, f);
  65.         stub_info.struct_length = size_of_info;
  66.         if (size_of_info > sizeof(StubInfo))
  67.           size_of_info = sizeof(StubInfo);
  68.         fseek(f, offset_of_info, 0);
  69.         fread(&stub_info, 1, (int)size_of_info, f);
  70.         fclose(f);
  71.         return;
  72.       }
  73.     }
  74.     else
  75.       key_p = 0;
  76.     key_cnt++;
  77.     if (key_cnt > key_max)
  78.       break;
  79.   }
  80.   fclose(f);
  81.   fprintf(stderr, "Error: I cannot find the stub info structure.  Must be either\n");
  82.   fprintf(stderr, "this is not a stub'd program, or it is older and does not have one.\n");
  83.   exit(1);
  84. }
  85.  
  86. main(int argc, char **argv)
  87. {
  88.   long info_ofs;
  89.   int stub_f;
  90.   char *stub_fname;
  91.   long size;
  92.   
  93.   if (argc > 1)
  94.     stub_fname = argv[1];
  95.   else
  96.     stub_fname = "stub.exe";
  97.  
  98.   stub_f = _open(stub_fname, O_RDONLY);
  99.   if (stub_f < 0)
  100.   {
  101.     fprintf(stderr, "Unable to open file %s\n", stub_fname);
  102.     perror("The error was");
  103.     exit(1);
  104.   }
  105.  
  106.   find_info(stub_fname);
  107.  
  108.   size = lseek(stub_f, -4L, 2);
  109.   _read(stub_f, &info_ofs, 4);
  110.  
  111.   if (info_ofs <= 0 || info_ofs > size || info_ofs != offset_of_info)
  112.   {
  113.     unsigned short header[2];
  114.     int stub_f = _open(stub_fname, O_WRONLY);
  115.     if (stub_f < 0)
  116.     {
  117.       fprintf(stderr, "Error: cannot write to %s\n", stub_fname);
  118.       perror("The error was");
  119.       exit(1);
  120.     }
  121.     size = lseek(stub_f, 0L, 2) + 4;
  122.     size = (size+511)&~511;
  123.     lseek(stub_f, size-4, 0);
  124.     _write(stub_f, &offset_of_info, 4);
  125.     header[0] = size & 511;
  126.     header[1] = (size + 511)/512;
  127.     lseek(stub_f, 2L, 0);
  128.     _write(stub_f, header, 4);
  129.     _close(stub_f);
  130.   }
  131.  
  132.   _close(stub_f);
  133.  
  134.   return 0;
  135. }
  136.